<template>
  <xtx-dialog title="(formData.id?'编辑':'添加')+收货地址" v-model:visible="dialogShow">
    <div class="address-add">

      <!-- 表单 -->
      <div class="xtx-form">
      <div class="xtx-form-item">
        <div class="label">收货人：</div>
        <div class="field">
          <input class="input" placeholder="请输入收货人" v-model="formData.receiver"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">手机号：</div>
        <div class="field">
          <input class="input" placeholder="请输入手机号" v-model="formData.contact"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地区：</div>
        <div class="field">
          <XtxCity placeholder="请选择所在地区" @change="changeCity" :fullLocation="formData.fullLocation"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">详细地址：</div>
        <div class="field">
          <input class="input" placeholder="请输入详细地址" v-model="formData.address"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">邮政编码：</div>
        <div class="field">
          <input class="input" placeholder="请输入邮政编码" v-model="formData.postalCode"/>
        </div>
      </div>
      <div class="xtx-form-item">
        <div class="label">地址标签：</div>
        <div class="field">
          <input class="input" placeholder="请输入地址标签，逗号分隔" v-model="formData.addressTags"/>
        </div>
      </div>
    </div>
    </div>

    <template v-slot:footer>
      <xtx-button type="primary" style="margin-right:20px;" @click="onsubmit">确定</xtx-button>
      <xtx-button type="gray">取消</xtx-button>
    </template>
  </xtx-dialog>
</template>

<script>
import { reactive, ref } from 'vue'
import { addAddress, editAddress } from '@/api/order.js'
import message from '@/components/library/message'
export default {
  name: 'AddressAdd',

  setup (props, { emit }) {
    const dialogShow = ref(false)

    const open = (address) => {
      // 先填充数据 - 编辑
      if (address.id) {
        for (const key in formData) {
          formData[key] = address[key]
        }
      } else {
        // 先清空数据 - 添加
        for (const key in formData) {
          if (key !== 'isDefault') {
            formData[key] = ''
          }
        }
      }
      dialogShow.value = true
    }

    // 表单数据

    const formData = reactive({
      id: '',
      receiver: '',
      contact: '',
      provinceCode: '',
      cityCode: '',
      countyCode: '',
      address: '',
      postalCode: '',
      addressTags: '',
      isDefault: 0,
      fullLocation: ''
    })

    // 子组件选择完毕后回调
    const changeCity = (data) => {
      formData.provinceCode = data.provinceCode
      formData.cityCode = data.cityCode
      formData.countyCode = data.countyCode
      formData.fullLocation = data.fullLocation
    }

    const onsubmit = () => {
      if (formData.id) {
        editAddress(formData).then(data => {
          message({ type: 'success', text: '修改地址成功' })

          dialogShow.value = false

          emit('on-success', formData)
        })
      } else {
        addAddress(formData).then(data => {
          message({ type: 'success', text: '添加地址成功' })

          formData.id = data.result.id

          dialogShow.value = false

          emit('on-success', formData)
        })
      }
    }

    return { dialogShow, open, formData, changeCity, onsubmit }
  }

}
</script>

<style lang="less" scoped>
.xtx-dialog {
  :deep(.wrapper){
    width: 780px;
    .body {
      font-size: 14px;
    }
  }
}
.xtx-form {
  padding: 0;
  input {
    outline: none;
    &::placeholder {
      color: #ccc;
    }
  }
}
.xtx-city {
  width: 320px;
  :deep(.select) {
    height: 50px;
    line-height: 48px;
    display: flex;
    padding: 0 10px;
    justify-content: space-between;
    .placeholder {
      color: #ccc;
    }
    i {
      color: #ccc;
      font-size: 18px;
    }
    .value {
      font-size: 14px;
    }
  }
  :deep(.option) {
    top: 49px;
  }
}
</style>
